package com.hp.mvc.service.impl;

import com.hp.mvc.pojo.Book;
import com.hp.mvc.service.BookService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    private List<Book> books = new ArrayList<>();

    {
        books.addAll(Arrays.asList(
                new Book(1L,"红楼梦","曹雪芹",88.0),
                new Book(2L,"水浒传","施耐庵",88.0),
                new Book(3L,"西游记","吴承恩",88.0),
                new Book(4L,"三国演义","罗贯中",88.0)));
    }



    @Override
    public List<Book> findAll() {
        return books;
    }

    @Override
    public Book findById(Long id) {
        return books.stream().filter(book -> book.getId() == id).findFirst().get();
    }

    @Override
    public void add(Book book) {
        books.add(book);
    }

    @Override
    public void update(Book book) {
        Book book1 = findById(book.getId());
        book1.setAuthor(book.getAuthor());
        book1.setName(book.getName());
        book1.setPrice(book.getPrice());
    }

    @Override
    public void delete(Long id) {
        Book book = findById(id);
        books.remove(book);
    }
}
